package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserSearchServiceImpl implements IUserSearchService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Async
    @Override
    public void saveRecord(Integer userId, String equipmentId, String keyword) {
       /* // 构建用户的搜索记录
        ApUserSearch apUserSearch = new ApUserSearch();
        if (userId != null) {
            apUserSearch.setUserId(userId);
        } else {
            apUserSearch.setEquipmentId(equipmentId);
        }
        apUserSearch.setKeyword(keyword);
        apUserSearch.setCreatedTime(new Date());
        // 判断该用户针对该关键字是否已有历史记录
        Query query = new Query();
        if (userId != null) {
            query.addCriteria(Criteria.where("userId").is(userId));
        } else {
            query.addCriteria(Criteria.where("equipmentId").is(equipmentId));
        }
        query.addCriteria(Criteria.where("keyword").is(keyword));
        boolean exists = mongoTemplate.exists(query, ApUserSearch.class);
        // 保存用户的搜索记录
        if (!exists) {
            mongoTemplate.save(apUserSearch);
        }*/

       ApUserSearch userSearch = new ApUserSearch();

       if(userId!=null){

           userSearch.setUserId(userId);
       }else {
           userSearch.setEquipmentId(equipmentId);
       }
       userSearch.setCreatedTime(new Date());

       userSearch.setKeyword(keyword);

       Query query = new Query();

       if(userId!=null){

           query.addCriteria(Criteria.where("userId").is(userId).and("keyword").is(keyword));
       }else {
           query.addCriteria(Criteria.where("equipmentId").is(equipmentId).and("keyword").is(keyword));
       }
        boolean exists = mongoTemplate.exists(query,ApUserSearch.class);

       if(!exists){

           mongoTemplate.save(userSearch);
       }
    }

    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
       /* // 默认加载用户查询最近的5条数据
        User user = AppThreadLocalUtil.get();
        Query query = new Query();
        if (user != null) {
            query.addCriteria(Criteria.where("userId").is(user.getUserId()));
        } else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }
        // 取5条数据
        query.with(PageRequest.of(0, 5, Sort.Direction.DESC, "createdTime"));
        List<ApUserSearch> apUserSearches = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(apUserSearches);*/
        User user = AppThreadLocalUtil.get();

        Query query = new Query();

        if(user!=null){

            query.addCriteria(Criteria.where("userId").is(user.getUserId())).with(PageRequest.of(0,5, Sort.Direction.DESC,"createdTime"));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId())).with(PageRequest.of(0,5, Sort.Direction.DESC,"createdTime"));
        }
        List<ApUserSearch> apUserSearches = mongoTemplate.find(query, ApUserSearch.class);

        return ResponseResult.okResult(apUserSearches);
    }

    @Override
    public ResponseResult del(ApArticleSearchDto dto) {
        /*List<ApUserSearch> hisList = dto.getHisList();
        List<String> ids = new ArrayList<>();
        for (ApUserSearch userSearch : hisList) {
            ids.add(userSearch.getId());
        }
        // 删除
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(ids));
        mongoTemplate.remove(query, ApUserSearch.class);
        return ResponseResult.okResult();*/

        List<ApUserSearch> hisList = dto.getHisList();

        List<String> ids = new ArrayList<>();

        for (ApUserSearch userSearch : hisList) {

            ids.add(userSearch.getId());
        }
        Query query = new Query();

        query.addCriteria(Criteria.where("id").is(ids));

        mongoTemplate.remove(query,ApUserSearch.class);

        return ResponseResult.okResult();
    }
}
